import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'cart.dart';
import 'notifier/cart_model.dart';

class CatalogWidget extends StatelessWidget {
  const CatalogWidget({ super.key });

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => CartModel(),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.yellow,
          title: Consumer<CartModel>(
            builder: (context, cart, child) {
              return Text(
                "Catalog ${ cart.items.length }",
                style: const TextStyle(fontWeight: FontWeight.bold),
              );
            },
          ),
          centerTitle: true,
          actions: [
            Container(
              padding: const EdgeInsets.only(right: 8.0),
              child: ElevatedButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all<Color>(Colors.yellow),
                  elevation: MaterialStateProperty.all<double>(0.0),
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const CartWidget())
                  );
                },
                child: const Icon(Icons.shopping_cart, color: Colors.black87,)
              )
            )
          ],
        ),
        body: ListView(
          children: List.generate(20, (index) {
            return ListTile(
              leading: Container(
                width: 20.0,
                height: 20.0,
                color: Color.fromARGB(255, index * 10, index * 10, index * 10),
              ),
              title: Text("item $index"),
              trailing: Consumer<CartModel>(
                builder: (context, cart, child) {
                  return ElevatedButton(
                    onPressed: () {
                      cart.add(index);
                    },
                    child: const Text("ADD")
                  );
                }
              ),
            );
          }),
        ),
      ),
    );
  }
}